package com.yonyou.pmclouds.reason.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.BuziExCodeConst;
import com.yonyou.pmclouds.reason.entity.ProblemReasonVO;
import com.yonyou.pmclouds.reason.mapper.ProblemReasonMapper;
import org.apache.commons.lang3.StringUtils;

public class ProblemReasonUpdateNameCheck implements SingleProcessor<ProblemReasonVO> {
    private ProblemReasonMapper mapper;

    public ProblemReasonUpdateNameCheck(ProblemReasonMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void process(ProblemReasonVO vo, ProblemReasonVO orig) throws BusinessException {
        if (StringUtils.equals(vo.getSname(), orig.getSname())) {
            return;
        }
        boolean nameUnique = mapper.countName(orig.getPkTenant(), vo.getSname(), vo.getCategory()) == 0;
        if (!nameUnique) {
            throw new BusinessException("已经存在相同的问题原因名称，请修改!", BuziExCodeConst.CODE_NAME_DUPLICATE);
        }
    }
}
